/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fwdisp.gui.tabs.settings;

import fwdisp.gui.moduls.IDTable;
import fwdisp.gui.moduls.SecRowPanel;
import settings.MainSettingsManager;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import settings.collections.SchleifenCollection;
import settings.collections.elements.Schleife;

/**
 *
 * @author rbs
 */
public class SchleifenSettingsPanel extends SecRowPanel{

    public SchleifenSettingsPanel() {
        setName("Schleifen");
        setVisible(true);
        
                setLayout(new BorderLayout(40, 40));

        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        String rics[][] = new String[][]{};

        String names[] = new String[]{"ID", "Schleifenname", "RIC"};

        final DefaultTableModel dtm = new DefaultTableModel(rics, names);

        final IDTable table = new IDTable(dtm);

        dtm.addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent tme) {
                
                int row = tme.getFirstRow();
                if (tme.getType() == TableModelEvent.UPDATE)
                {
                    MainSettingsManager setting = MainSettingsManager.getSetting();
                    SchleifenCollection schleifenCollection = setting.getSchleifenCollection();
                    Schleife old_schleife = schleifenCollection.getSchleife(tme.getFirstRow());
                    Schleife new_schleife = new Schleife(row, (String) dtm.getValueAt(row, 1), (String) dtm.getValueAt(row, 2));
                    schleifenCollection.changeSchleife(old_schleife, new_schleife);
                }
            }
        });
        
        //adding cars from file:
        ArrayList<Schleife> schleifen = MainSettingsManager.getSetting().getSchleifenCollection().getSchleifen();
        for (Schleife schleife: schleifen)
        {
            String[] row = {"" + schleife.getId(), schleife.getName(), schleife.getRic()};
            dtm.addRow(row); 
        }
        
        
        //table.setEnabled(false);
        add(new JScrollPane(table), BorderLayout.CENTER);

        JPanel new_object = new JPanel(new GridLayout(1, 3, 20, 0));

        JButton add = new JButton("+");
        add.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                SchleifenCollection schleifenCollection = MainSettingsManager.getSetting().getSchleifenCollection();
                schleifenCollection.addschleife(new Schleife(table.getRowCount(), "", ""));
                dtm.addRow(new String[]{"" + (dtm.getRowCount()), "", ""});
            }
        });
        
        JButton sub = new JButton("-");
        sub.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                int selRow = table.getSelectedRow();
                if (selRow == -1)
                    if (table.getRowCount() > 0)
                        selRow = table.getRowCount() -1;
                    else
                        return;
                for (int i = selRow + 1; i < table.getRowCount(); i++)
                {
                    dtm.setValueAt(i - 1, i, 0);
                }
                SchleifenCollection schleifenCollection = MainSettingsManager.getSetting().getSchleifenCollection();
                schleifenCollection.delSchleife(selRow);
                
                dtm.removeRow(selRow);
                schleifenCollection.printAllSchleifen();
            }
        });
        
        new_object.add(sub);
        new_object.add(add);
        new_object.setBackground(getBackground());
        add(new_object, BorderLayout.SOUTH);
    }


}
